package com.mythesis.bpel.ui.client.wrapper;


import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.widgetideas.graphics.client.GWTCanvas;
import com.mythesis.bpel.ui.client.controls.BpelControl;
import com.mythesis.bpel.ui.client.eventhandler.ControlClickHandler;
import com.mythesis.bpel.ui.client.eventhandler.ControlKeydownHandler;

public abstract class ControlWrapper extends FocusPanel {
	
	protected BpelControl control;
	protected Boundary controlOutline;
	protected AbsolutePanel outlinePanel;
	protected GWTCanvas canvas;
	protected boolean focus;
	
	public ControlWrapper(BpelControl control){
		this.setControl(control);
		this.controlOutline = new Boundary();
		this.outlinePanel = new AbsolutePanel();
		this.canvas = new GWTCanvas(); 
		this.focus = false;
		addClickHandler(new ControlClickHandler());
		addKeyDownHandler(new ControlKeydownHandler());
	}
	
	public abstract void addControl(ControlWrapper wrapper);
	public abstract void removeControl(ControlWrapper wrapper);
	public abstract void calculateOutline();
	public abstract void renderControl();
	public abstract void renderFocus();
	public abstract Boundary getOuline();

	public void setControl(BpelControl control) {
		this.control = control;
	}

	public BpelControl getControl() {
		return control;
	}
	
	public Boundary getControlOutline() {
		return controlOutline;
	}

	public void setControlOutline(Boundary controlOutline) {
		this.controlOutline = controlOutline;
	}
	
	@Override
	public HandlerRegistration addClickHandler(ClickHandler handler) {
		// TODO Auto-generated method stub
		if(handler instanceof ControlClickHandler){
			((ControlClickHandler)handler).setControlWrapper(this);
		}
		return super.addClickHandler(handler);
	}
	
	
	
	@Override
	public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
		// TODO Auto-generated method stub
		if(handler instanceof ControlKeydownHandler){
			((ControlKeydownHandler)handler).setControlWrapper(this);
		}
		return super.addKeyDownHandler(handler);
	}

	public class Boundary {
		protected int x;
		protected int y;
		protected int w;
		protected int h;
		
		public Boundary(){
			this.x = 0;
			this.y = 0;
			this.w = 0;
			this.h = 0;
		}
		
		public int getX() {
			return x;
		}
		public void setX(int x) {
			this.x = x;
		}
		public int getY() {
			return y;
		}
		public void setY(int y) {
			this.y = y;
		}
		public int getWidth() {
			return w;
		}
		public void setWidth(int width) {
			this.w = width;
		}
		public int getHeight() {
			return h;
		}
		public void setHeight(int height) {
			this.h = height;
		}
	}
}
